# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from packagetool import PackageTool

test_data = [
    1,
    123,
    97,
    210,
    2,
    150,
    73,
    21,
    205,
    91,
    7,
    218,
    15,
    73,
    64,
    74,
    216,
    18,
    77,
    251,
    33,
    9,
    64,
    0,
    128,
    0,
    0,
    0,
    128,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    128,
    128,
    13,
    0,
    0,
    0,
    72,
    101,
    108,
    108,
    111,
    44,
    32,
    119,
    111,
    114,
    108,
    100,
    33,
    210,
    2,
    150,
    73,
    21,
    205,
    91,
    7,
    255,
    255,
    255,
    255,
    255,
    255,
    255,
    255,
    255,
    255,
    255,
    255,
    255,
    255,
    255,
    3,
    0,
    0,
    0,
    1,
    0,
    1,
    3,
    0,
    0,
    0,
    1,
    2,
    3,
    3,
    0,
    0,
    0,
    97,
    98,
    99,
    2,
    0,
    0,
    0,
    2,
    0,
    0,
    0,
    1,
    0,
    0,
    0,
    4,
    0,
    0,
    0,
    3,
    0,
    0,
    0,
    2,
    0,
    0,
    0,
    218,
    15,
    73,
    64,
    84,
    248,
    45,
    64,
    2,
    0,
    0,
    0,
    74,
    216,
    18,
    77,
    251,
    33,
    9,
    64,
    1,
    127,
    66,
    135,
    10,
    191,
    5,
    64,
    3,
    0,
    0,
    0,
    0,
    128,
    1,
    128,
    255,
    127,
    3,
    0,
    0,
    0,
    0,
    0,
    0,
    128,
    1,
    0,
    0,
    128,
    255,
    255,
    255,
    127,
    3,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    128,
    1,
    0,
    0,
    0,
    0,
    0,
    0,
    128,
    255,
    255,
    255,
    255,
    255,
    255,
    255,
    127,
    3,
    0,
    0,
    0,
    128,
    129,
    127,
    2,
    0,
    0,
    0,
    13,
    0,
    0,
    0,
    72,
    101,
    108,
    108,
    111,
    44,
    32,
    119,
    111,
    114,
    108,
    100,
    33,
    18,
    0,
    0,
    0,
    228,
    189,
    160,
    229,
    165,
    189,
    239,
    188,
    140,
    228,
    184,
    150,
    231,
    149,
    140,
    239,
    188,
    129,
    2,
    0,
    0,
    0,
    6,
    0,
    0,
    0,
    5,
    0,
    0,
    0,
    8,
    0,
    0,
    0,
    7,
    0,
    0,
    0,
    3,
    0,
    0,
    0,
    255,
    255,
    254,
    255,
    0,
    0,
    3,
    0,
    0,
    0,
    255,
    255,
    255,
    255,
    254,
    255,
    255,
    255,
    0,
    0,
    0,
    0,
    3,
    0,
    0,
    0,
    255,
    255,
    255,
    255,
    255,
    255,
    255,
    255,
    254,
    255,
    255,
    255,
    255,
    255,
    255,
    255,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    3,
    0,
    0,
    0,
    255,
    254,
    0,
]
print(len(test_data))
test_data = bytearray(test_data)

if __name__ == "__main__":
    pl = PackageTool()
    pl.put_bool(True)
    pl.put_byte(123)
    pl.put_char("a")
    pl.put_duration({"nsecs": 123456789, "secs": 1234567890})
    pl.put_float32(3.1415926)
    pl.put_float64(3.1415926)
    pl.put_int16(-32768)
    pl.put_int32(-2147483648)
    pl.put_int64(-9223372036854775808)
    pl.put_int8(-128)
    pl.put_string("Hello, world!")
    pl.put_time({"nsecs": 123456789, "secs": 1234567890})
    pl.put_uint16(65535)
    pl.put_uint32(4294967295)
    pl.put_uint64(18446744073709551615)
    pl.put_uint8(255)

    pl.put_bool_array([True, False, True])
    pl.put_byte_array([1, 2, 3])
    pl.put_char_array(["a", "b", "c"])
    pl.put_duration_array([{"nsecs": 1, "secs": 2}, {"nsecs": 3, "secs": 4}])
    pl.put_float32_array([3.1415926, 2.7182818])
    pl.put_float64_array([3.1415926, 2.7182818])
    pl.put_int16_array([-32768, -32767, 32767])
    pl.put_int32_array([-2147483648, -2147483647, 2147483647])
    pl.put_int64_array(
        [-9223372036854775808, -9223372036854775807, 9223372036854775807]
    )
    pl.put_int8_array([-128, -127, 127])
    pl.put_string_array(["Hello, world!", "你好，世界！"])
    pl.put_time_array([{"nsecs": 5, "secs": 6}, {"nsecs": 7, "secs": 8}])
    pl.put_uint16_array([65535, 65534, 0])
    pl.put_uint32_array([4294967295, 4294967294, 0])
    pl.put_uint64_array([18446744073709551615, 18446744073709551614, 0])
    pl.put_uint8_array([255, 254, 0])

    t = pl.get_bool()
    if t != True:
        print("!!!! get_bool error")
    t = pl.get_byte()
    if t != 123:
        print("!!!! get_byte error")
    t = pl.get_char()
    if t != "a":
        print("!!!! get_char error")
    t = pl.get_duration()
    if t != {"nsecs": 123456789, "secs": 1234567890}:
        print("!!!! get_duration error")
    t = pl.get_float32()
    if t != 3.1415926:
        print("!!!! get_float32 error", t, 3.1415926)
    t = pl.get_float64()
    if t != 3.1415926:
        print("!!!! get_float64 error")
    t = pl.get_int16()
    if t != -32768:
        print("!!!! get_int16 error")
    t = pl.get_int32()
    if t != -2147483648:
        print("!!!! get_int32 error")
    t = pl.get_int64()
    if t != -9223372036854775808:
        print("!!!! get_int64 error")
    t = pl.get_int8()
    if t != -128:
        print("!!!! get_int8 error")
    t = pl.get_string()
    if t != "Hello, world!":
        print("!!!! get_string error")
    t = pl.get_time()
    if t != {"nsecs": 123456789, "secs": 1234567890}:
        print("!!!! get_time error")
    t = pl.get_uint16()
    if t != 65535:
        print("!!!! get_uint16 error")
    t = pl.get_uint32()
    if t != 4294967295:
        print("!!!! get_uint32 error")
    t = pl.get_uint64()
    if t != 18446744073709551615:
        print("!!!! get_uint64 error")
    t = pl.get_uint8()
    if t != 255:
        print("!!!! get_uint8 error")

    t = pl.get_bool_array()
    if t != [True, False, True]:
        print("!!!! get_bool_array error")
    t = pl.get_byte_array()
    if t != [1, 2, 3]:
        print("!!!! get_byte_array error")
    t = pl.get_char_array()
    if t != ["a", "b", "c"]:
        print("!!!! get_char_array error")
    t = pl.get_duration_array()
    if t != [{"nsecs": 1, "secs": 2}, {"nsecs": 3, "secs": 4}]:
        print("!!!! get_duration_array error")
    t = pl.get_float32_array()
    if t != [3.1415926, 2.7182818]:
        print("!!!! get_float32_array error", t, [3.1415926, 2.7182818])
    t = pl.get_float64_array()
    if t != [3.1415926, 2.7182818]:
        print("!!!! get_float64_array error")
    t = pl.get_int16_array()
    if t != [-32768, -32767, 32767]:
        print("!!!! get_int16_array error")
    t = pl.get_int32_array()
    if t != [-2147483648, -2147483647, 2147483647]:
        print("!!!! get_int32_array error")
    t = pl.get_int64_array()
    if t != [-9223372036854775808, -9223372036854775807, 9223372036854775807]:
        print("!!!! get_int64_array error")
    t = pl.get_int8_array()
    if t != [-128, -127, 127]:
        print("!!!! get_int8_array error")
    t = pl.get_string_array()
    if t != ["Hello, world!", "你好，世界！"]:
        print("!!!! get_string_array error")
    t = pl.get_time_array()
    if t != [{"nsecs": 5, "secs": 6}, {"nsecs": 7, "secs": 8}]:
        print("!!!! get_time_array error")
    t = pl.get_uint16_array()
    if t != [65535, 65534, 0]:
        print("!!!! get_uint16_array error")
    t = pl.get_uint32_array()
    if t != [4294967295, 4294967294, 0]:
        print("!!!! get_uint32_array error")
    t = pl.get_uint64_array()
    if t != [18446744073709551615, 18446744073709551614, 0]:
        print("!!!! get_uint64_array error")
    t = pl.get_uint8_array()
    if t != [255, 254, 0]:
        print("!!!! get_uint8_array error")

    for i in range(len(test_data)):
        # print("%d\t%d\t%d"%(i, pl.buffer[i] , test_data[i]))
        if pl.buffer[i] != test_data[i]:
            print("!!!! %d\t%d\t%d" % (i, pl.buffer[i], test_data[i]))
            break
    print("test ok", len(pl.buffer))
